﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Editeur_map;


namespace lapinou_game2
{
    /// <summary>
    /// A modifier pour gérer tous les chargements de map, d'évennements, etc ..
    /// </summary>
    class MapGame
    {
        
        Random rnd = new Random();

        Map_State CurrentMap = new Map_State();
        List<Sprite> elements = new List<Sprite>();
        // background
        Sprite back = new Sprite(0, 0);

        Sound sound;

        int vitesseDefilement = 5;
        int x_decalage = 0, y_decalage = 0;

        ContentManager content;
        GraphicsDeviceManager graphics;

        public MapGame(ContentManager Content, GraphicsDeviceManager Graphics)
        {
            content = Content;
            graphics = Graphics;            
            sound = new Sound();

            CurrentMap = LoadMap("map/map1.rab");
        }

        public Map_State getCurrentMap()
        {
            return CurrentMap;
        }

        public void LoadContent()
        {

        }

        public void Initialize()
        {
            // elements
            foreach(Node node in CurrentMap.list_node)
            {
                Sprite sprite = new Sprite(node.pos.X + node.x_decalage, node.pos.Y + node.y_decalage);
                sprite.LoadContent(content, @"image\" +  path_elt(node.path_elt));
                elements.Add(sprite);
            }

            // background
            back.LoadContent(content, @"image\backgrounds\" + path_back(CurrentMap.background_list[0].path_back));
        }

        public void Update(Rab rab, GraphicsDeviceManager graphics, KeyboardState kb)
        {
            // SCROLLING \\


            // Left
            if (rab.getpos().X < 120 && kb.IsKeyDown(Keys.Left))
            {
                x_decalage += vitesseDefilement;
                rab.setRabPosition(Convert.ToInt32(rab.getpos().X + vitesseDefilement), Convert.ToInt32(rab.getpos().Y));
                foreach (Sprite sprite in elements)
                {
                    rab.reLoadBounding(x_decalage, y_decalage);                                     
                    sprite.setPos(Convert.ToInt32(sprite.Position.X + vitesseDefilement), Convert.ToInt32(sprite.Position.Y));
                }
            }
            // Right
            if (rab.getpos().X > (graphics.PreferredBackBufferWidth - 120) && kb.IsKeyDown(Keys.Right))
            {
                x_decalage -= vitesseDefilement;
                rab.setRabPosition(Convert.ToInt32(rab.getpos().X - vitesseDefilement), Convert.ToInt32(rab.getpos().Y));
                foreach (Sprite sprite in elements)
                {
                    rab.reLoadBounding(x_decalage, y_decalage);    
                    sprite.setPos(Convert.ToInt32(sprite.Position.X - vitesseDefilement), Convert.ToInt32(sprite.Position.Y));
                }
            }
            // Up
            if (rab.getpos().Y < 120 && kb.IsKeyDown(Keys.Up))
            {
                y_decalage += vitesseDefilement * 10;
                rab.setRabPosition(Convert.ToInt32(rab.getpos().X), Convert.ToInt32(rab.getpos().Y + vitesseDefilement));
                foreach (Sprite sprite in elements)
                {
                    rab.reLoadBounding(x_decalage, y_decalage);    
                    sprite.setPos(Convert.ToInt32(sprite.Position.X), Convert.ToInt32(sprite.Position.Y + vitesseDefilement));
                }
            }
            // Down
            if (rab.getpos().Y > (graphics.PreferredBackBufferHeight - 120))
            {
                y_decalage -= vitesseDefilement;
                rab.setRabPosition(Convert.ToInt32(rab.getpos().X), Convert.ToInt32(rab.getpos().Y - vitesseDefilement));
                foreach (Sprite sprite in elements)
                {
                    rab.reLoadBounding(x_decalage, y_decalage);    
                    sprite.setPos(Convert.ToInt32(sprite.Position.X), Convert.ToInt32(sprite.Position.Y - vitesseDefilement));
                }
            }
            
        }


        public void Draw(SpriteBatch sb)
        {
            back.Draw(sb, new Vector2((int)graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));

            foreach(Sprite sprite in elements)
                sprite.Draw(sb);
        }


        // Charge la map à partir du fichier .rab
        public Map_State LoadMap(string name)
        {
            Map_State map = new Map_State();
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(name, FileMode.OpenOrCreate);

            map = (Map_State)bf.Deserialize(fs);            

            return map;
        }

        public string path_elt(string path_elt)
        {
            string res = "";
            int i = 9;

            while (i < path_elt.Length && path_elt[i] != '.')
            {
                res += path_elt[i];
                i++;
            }


            return res;
        }

        public string path_back(string path_back)
        {
            string res = "";
            int i = 12;

            while (i < path_back.Length && path_back[i] != '.')
            {
                res += path_back[i];
                i++;
            }


            return res;
        }

    }
}
